(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	var now = 0;
	var Subject = null;//科目数据
	var Index = 0;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initGradeClass();
			
		});
	});
	function initGradeClass(){
		var cont = "";
		var classcont = $(".classes-choice",parent.document);
		if(classcont.length > 0){
			classcont.each(function(i){
				if(i == 0){
					cont += "<option value='"+$(this).attr("classcode")+"' type='"+$(this).attr("type")+"' grade='"+$(this).attr("grade")+"' classes='"+$(this).attr("class_")+"' selected='selected'>"+$(this).text()+"</option>";
				}else{
					cont += "<option value='"+$(this).attr("classcode")+"' type='"+$(this).attr("type")+"' grade='"+$(this).attr("grade")+"' classes='"+$(this).attr("class_")+"'>"+$(this).text()+"</option>";
				}
			});
		}else{
			cont += "<option value='' type=''>班级</option>";
		}
		$("#exam-classcode").html(cont);
		
		initExam(1,pageSize);
		
		$("#exam-classcode").unbind("change");
		$("#exam-classcode").change(function(){
			initExam(1,pageSize);
		});
	}
	function initExam(currentpage,pagesize){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var starttime = strToDate($("#start-time").val() + ":00")/1000;
		var endtime = strToDate($("#end-time").val() + ":00")/1000;
		var schooltype = $("#exam-classcode option:selected").attr("type");
		var classcode = $("#exam-classcode option:selected").val();
		var obj={
				schoolcode: schoolcode,
				classcode: classcode,
				schooltype: schooltype,
				currentpage: currentpage,
				pagesize: pagesize,
		};
		if($("#start-time").val() != "" && $("#end-time").val() != ""){
			obj.stime = starttime;
			obj.etime = endtime;
			$("#judge-time").hide();
		}else if($("#start-time").val() == "" && $("#end-time").val() == ""){
			$("#judge-time").hide();
		}else{
			$("#judge-time").text("请把时间填充完整");
			$("#judge-time").show();
			return false;
		}
			
		var data = {
				cmd: "getTeahcerAllExamInfo",
				model: "bigexam",
				userKey: userKey,
				data: obj,
				remark: "",
				
		};
		processBegin("正在运行中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			var pagecontent = "<div class=\"pages-btn-next\">&gt;&gt;</div>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("查询成功");
				var data = res.data.data;
				if(data.length > 0){
					var total = res.data.total;
					totalSize = Math.ceil(total/pagesize);
					if(totalSize <= 5 && totalSize >0){
						for(var i = totalSize-1 ; i >= 0; i--){
							if(i == currentpage-1){
								pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
							}else{
								pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
							}
						}
					}else if(totalSize == 0){
						pagecontent += "<div class=\"pages-btn\">1</div>";
					}else if(totalSize > 5){
						var lastp = (now*5)+5;
						if(lastp > totalSize){
							lastp = totalSize;
						}
						for(var i = lastp-1 ; i >= (now*5); i--){
							if(i == currentpage-1){
								pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
							}else{
								pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
							}
						}
					}
					pagecontent += "<div class=\"pages-btn-last\">&lt;&lt;</div>";
					$("#student-page").empty();
					$("#student-page").append(pagecontent);
					
					$(".pages-btn").unbind("click");
					$(".pages-btn").click(function(){
						$(".pages-btn").removeClass("page-btn-select");
						$(this).addClass("page-btn-select");
						currentPage = parseInt($(this).text());
						initExam(currentPage,pageSize);
					});
					
					$(".pages-btn-last").unbind("click");
					$(".pages-btn-last").click(function(){
						if(totalSize <= 5 && totalSize >0){
							return false;
						}else if(totalSize == 0){
							return false;
						}else if(totalSize > 5){
							if(now > 0){
								now --;
								initExam((now*5+1),pageSize);
							}else{
								return false;
							}
						}
					});
					
					$(".pages-btn-next").unbind("click");
					$(".pages-btn-next").click(function(){
						if(totalSize <= 5 && totalSize >0){
							return false;
						}else if(totalSize == 0){
							return false;
						}else if(totalSize > 5){
							if(totalSize%5 == 0){
								if((now+1) < totalSize/5){
									now ++;
									initExam((now*5+1),pageSize);
								}else{
									return false;
								}
							}else{
								if((now+1) <= (totalSize-totalSize%5)/5){
									now ++;
									initExam((now*5+1),pageSize);
								}else{
									return false;
								}
							}
						}
					});
					if(data.length > 0){
						for(var i = 0; i < data.length; i++){
							for(var key in data[i]){
								if(data[i][key] == null || data[i][key] == "null" || data[i][key] == undefined){
									data[i][key] = "";
								}
							}
							content += "<div class=\"table-trs\">" +
								"<div class=\"tables-left\" style='margin-left:15px;width:320px;'>" +
									"<div class=\"table-name\" style='width:320px;' coursename='"+data[i].coursename+"' examcode=\""+data[i].examcode+"\" grade=\""+data[i].grades+"\" schooltype=\""+data[i].schooltype+"\" title='"+data[i].examname+"'><a href='javascript:void(0)' class='into-exam-detail'>"+substr(data[i].examname,15) + "</a></div>" +
									"<div class=\"table-code\" style='width:320px;' title='"+data[i].describe+"'>"+substr(data[i].describe,20)+"</div>" +
								"</div>" +
								"<div class=\"tables-right\">" +
									"<div class=\"table-address\">创建于:"+getFormatDateByLong(parseInt(data[i].createtime)*1000,"yyyy-MM-dd hh:mm")+"</div>" +
									"<div class=\"table-oprate\">" +
										"<div class=\"table-ranking\" dataindex = \""+i+"\" examcode=\""+data[i].examcode+"\" examname=\""+data[i].examname+"\" grade=\""+data[i].grades+"\" schooltype=\""+data[i].schooltype+"\" title=\"排名\" style='margin-left:75px;'></div>" +
										"<div class=\"table-detail\" dataindex = \""+i+"\" examcode=\""+data[i].examcode+"\" title=\"详情\" style='margin-left:15px;'></div>" +
									"</div>" +
								"</div>" +
							"</div>";
						}
						$("#exam-data-show").empty();
						$("#exam-data-show").append(content);
						
						$(".table-trs").unbind("hover");
						$(".table-trs").hover(function(){
							$(this).find(".table-oprate").show();
						},function(){
							$(".table-oprate").hide();
						});
						
						$(".table-name").unbind("click");
						$(".table-name").click(function(){
							var examcode = $(this).attr("examcode");
							var examname = $(this).attr("title");
							var grade = $(this).attr("grade");
							var schooltype = $(this).attr("schooltype");
							$("#result-form-show").contents().find(".table-title").text(examname + "总榜");
							$("#result-form-show").show();
							$("#right-tables").hide();
							$(".title-search").hide();
							$("#result-form-show").contents().find("#back-to-table").unbind("click");
							$("#result-form-show").contents().find("#back-to-table").click(function(){
								$("#result-form-show").hide();
								$("#right-tables").show();
								$(".title-search").show();
							});						
							getExamResult(examcode,examname,grade,schooltype);
						});
						
						$(".table-ranking").unbind("click");
						$(".table-ranking").click(function(){
							var examcode = $(this).attr("examcode");
							var examname = $(this).attr("examname");
							var grade = $(this).attr("grade");
							var schooltype = $(this).attr("schooltype");
							$("#ranking-form-show").show();
							$("#right-tables").hide();
							$(".title-search").hide();
							$("#ranking-form-show").contents().find("#table-title").html(examname + "总榜");
							initRanking(examcode,grade,schooltype);
							$("#ranking-form-show").contents().find("#back-to-table").unbind("click");
							$("#ranking-form-show").contents().find("#back-to-table").click(function(){
								$("#right-tables").show();
								$("#ranking-form-show").hide();
								$(".title-search").show();
							});
						});
						
						$(".table-detail").unbind("click");
						$(".table-detail").click(function(){
							var flag = "detail";
							var examcode = $(this).attr("examcode");
							getExamDetail(examcode,flag);
							
							$("#exam-form-show").contents().find("#back-to-table").unbind("click");
							$("#exam-form-show").contents().find("#back-to-table").click(function(){
								$("#exam-form-show").hide();
								$("#right-tables").show();
								Index = 0;
							});
						});
					}else{
						content += "<div class=\"table-trs\">" +
										"<div class=\"no-data-show\">没有查到相关结果！</div>" +
									"</div>";
						$("#exam-data-show").empty();
						$("#exam-data-show").append(content);
					}
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#exam-data-show").empty();
					$("#exam-data-show").append(content);
				}
			}else{
				processError("查询失败");
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#exam-data-show").empty();
				$("#exam-data-show").append(content);
			}
			initClassEdit();
		},null,null);	
	}
	
	function initClassEdit(){
		var type = $("#exam-classcode option:selected").attr("type");
		var grade = $("#exam-classcode option:selected").attr("grade");
		var classes = $("#exam-classcode option:selected").attr("classes");
		var classcode = $("#exam-classcode option:selected").val();
		var name = "";
		if(type == "0"){
			name = "幼儿园";
		}else if(type == "1"){
			name = "小学";
		}else if(type == "2"){
			name = "中学";
		}else if(type == "3"){
			name = "高中";
		}
		var typeOpt = "<option value='"+type+"' selected='selected'>"+name+"</option>";
		$("#exam-form-show").contents().find("#exam-type").html(typeOpt);
		
		var gradeOpt = "<option value='"+grade+"' selected='selected'>"+grade+"年级</option>";
		$("#exam-form-show").contents().find("#exam-grade").html(gradeOpt);
		
		var classOpt = "<div class='classcode-item'><input type='checkbox' value='"+classcode+"' classes='"+classes+"' checked='checked'><span>"+classes+"班</span></div>";
		$("#exam-form-show").contents().find("#exam-classcode").html(classOpt);
		
		getAllCourseSubject();
	}
	
	
	
	function getAllCourseSubject(){
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var type = $("#exam-form-show").contents().find("#exam-type option:selected").val();
		var data = {
				cmd:"getAllCourseSubject",
				model:"course",
				userKey:userKey,
				data:{
					type:type,
					schoolcode:schoolcode
				},
				remark:"",
				
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1){
				var data = res.data;
				Subject = data;
				var cont = "<option value='' coursename=''>科目</option>";
				for(var i = 0; i < data.length; i++){
					cont += "<option value='"+data[i].subjectcode+"' coursename='"+data[i].coursecontent+"'>"+data[i].coursecontent+"</option>";
				}
				$("#exam-form-show").contents().find(".exam-coursename").html(cont);
			}
		},null,null);
	}
	function getExamDetail(examcode,flag){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $("#exam-classcode option:selected").val();
		var data = {
				cmd:"getExamDetial",
				model:"bigexam",
				userKey:userKey,
				data:{
					examcode:examcode,
					schoolcode:schoolcode,
					classcode:classcode
				},
				remark:"",
		};
		AjaxCrud.get2(serverUrl.ServerLoginUrl,data,function(res){
			showData(res,flag);
		},null,null);
	}
	
	function showData(res,type){
		$("#exam-form-show").show();
		$("#right-tables").hide();
		if(type == "detail"){
			$("#exam-form-show").contents().find("#save-btns-content").hide();
		}else if(type == "edit"){
			$("#exam-form-show").contents().find("#save-btns-content").show();
		}
		$("#exam-form-show").contents().find("#add-info-cont").html("");
		if(res.result == 1 && res.data != undefined && res.data != ""){
			var data = res.data[0];
			$("#exam-form-show").contents().find("#exam-examname").val(data.examname);
			$("#exam-form-show").contents().find("#exam-describe").val(data.describe);
			
			var list = data.courselist;
			if(list.length > 0){
				for(var i = 0; i < list.length; i ++){
					addCourcont(list[i],i);
					$("#exam-form-show").contents().find(".exam-coursename" + i + " option").each(function(){
						if(this.value == list[i].coursecode){
							$(this).attr("selected",true);
							return false;
						}
					});
				}
			}
		}else{
			processError("查询失败");
			addCourcont1();
		}
		if(type == "detail"){
			$("#exam-form-show").contents().find(".btn-cont").hide();
			$("#exam-form-show").contents().find("input").attr("disabled","disabled");
			$("#exam-form-show").contents().find("input").css("background-color","#E5E5E5");
			$("#exam-form-show").contents().find("select").attr("disabled","disabled");
			$("#exam-form-show").contents().find("select").css("background-color","#E5E5E5");
			$("#exam-form-show").contents().find("textarea").attr("disabled","disabled");
			$("#exam-form-show").contents().find("textarea").css("background-color","#E5E5E5");
			$("#exam-form-show").contents().find("#exam-classcode").css("background-color","#E5E5E5");
		}else if(type == "edit"){
			$("#exam-form-show").contents().find(".btn-cont").show();
			$("#exam-form-show").contents().find(".table-middle input").removeAttr("disabled");
			$("#exam-form-show").contents().find(".table-middle input").css("background-color","#fff");
			$("#exam-form-show").contents().find("select").removeAttr("disabled");
			$("#exam-form-show").contents().find("select").css("background-color","#fff");
			$("#exam-form-show").contents().find("textarea").removeAttr("disabled");
			$("#exam-form-show").contents().find("textarea").css("background-color","#fff");
			$("#exam-form-show").contents().find("#exam-classcode").css("background-color","#fff");
		}
	}
	
	function addCourcont(data,index){
		var cont = "<div class='add-info-edit info-edit"+index+"' index='"+index+"'>" +
				"<input type='text' class='Wdate exam-stime' readonly='readonly' value='"+getFormatDateByLong(parseInt(data.starttime)*1000,"yyyy-MM-dd hh:mm")+"'/>" +
				"<input type='text' class='Wdate exam-etime' readonly='readonly' value='"+getFormatDateByLong(parseInt(data.endtime)*1000,"yyyy-MM-dd hh:mm")+"'/>" +
				"<select class='exam-coursename"+index+"'><option  value='' coursename=''>科目</option></select>" +
				"<input type='text' placeholder='满分值' style='width:100px;' value='"+data.score+"'>" +
				"<div class='btn-cont btn-add btn-edit"+index+"' index='"+index+"'>+</div>" +
				"<span class='info-error' id='info-error"+index+"' index='"+index+"'></span>" +
			"</div>";
		$("#exam-form-show").contents().find("#add-info-cont").append(cont);
		
		var cont = "<option value='' coursename=''>科目</option>";
		for(var i = 0; i < Subject.length; i++){
			cont += "<option value='"+Subject[i].subjectcode+"' coursename='"+Subject[i].coursecontent+"'>"+Subject[i].coursecontent+"</option>";
		}
		$("#exam-form-show").contents().find(".exam-coursename" + index).html(cont);
		
		if(index > 0){
			$("#exam-form-show").contents().find(".btn-edit" + (index-1)).removeClass("btn-add").addClass("btn-del");
			$("#exam-form-show").contents().find(".btn-edit" + (index-1)).html("-");
		}
		Index ++;
	}
	
	function addCourcont1(){
		var cont = "<div class='add-info-edit info-edit"+Index+"' index='"+Index+"'>" +
						"<input type='text' class='Wdate exam-stime' readonly='readonly' />" +
						"<input type='text' class='Wdate exam-etime' readonly='readonly'/>" +
						"<select class='exam-coursename"+Index+"' ><option value='' coursename=''>科目</option></select>" +
						"<input type='text' placeholder='满分值' style='width:100px;'>" +
						"<div class='btn-cont btn-add btn-edit"+Index+"' index='"+Index+"'>+</div>" +
						"<span class='info-error' id='info-error"+Index+"' index='"+Index+"'></span>" +
					"</div>";
		$("#exam-form-show").contents().find("#add-info-cont").append(cont);
		
		var cont = "<option value='' coursename=''>科目</option>";
		for(var i = 0; i < Subject.length; i++){
			cont += "<option value='"+Subject[i].subjectcode+"' coursename='"+Subject[i].coursecontent+"'>"+Subject[i].coursecontent+"</option>";
		}
		$("#exam-form-show").contents().find(".exam-coursename" + Index).html(cont);
		
		if(Index > 0){
			$("#exam-form-show").contents().find(".btn-edit" + (Index-1)).removeClass("btn-add").addClass("btn-del");
			$("#exam-form-show").contents().find(".btn-edit" + (Index-1)).html("-");
		}
		Index++;
	}
	
	//考试排名信息
	function initRanking(examcode,grade,schooltype){
		processBegin("正在运行中···");
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var rolecode = $("#user-name",parent.parent.document).attr("rolecodes");
		var classcode = $("#exam-classcode option:selected").val();
		var data = {
				cmd:"getExamTotalList",
				model:"bigexam",
				userKey:userKey,
				data:{
					examcode:examcode,
					classcode:classcode,
					schoolcode:schoolcode,
					rolecode:rolecode,
					grades:grade,
				},
				remark:"",
				
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("查询成功");
				var data = res.data.data;
				if(data.length > 0){
					var subjTitcont = "";
					var subjinfo = data[0].subjectinfo;
					for(var i = 0; i < subjinfo.length; i++){
						subjTitcont += "<td>"+subjinfo[i].subject+"</td>";
					}
					content += "<table class='ranking-table'><tr><td>姓名</td><td>学号</td><td>班级</td>"+subjTitcont+"<td>总分</td><td>班排名</td><td>校排名</td></tr>";
					for(var i = 0; i < data.length; i++){
						var subjcont = "";
						var subjArr = data[i].subjectinfo;
						for(var k = 0; k < subjArr.length; k++){
							subjcont += "<td>"+subjArr[k].score+"</td>";
						}
						content += "<tr><td>"+data[i].studentname+"</td><td>"+data[i].studentcode+"</td><td>"+data[i].classesname+"</td>"+subjcont+"<td>"+data[i].scorenumber+"</td><td>"+data[i].classranking+"</td><td>"+data[i].schoolrank+"</td></tr>";
					}
					content += "</table>";
					$("#ranking-form-show").contents().find("#result-data-show").empty();
					$("#ranking-form-show").contents().find("#result-data-show").append(content);
					
					var cdsH =$("#ranking-form-show").height() - $("#ranking-form-show").contents().find(".content-title").height() - 40;
					$("#ranking-form-show").contents().find(".content-data-show").height(cdsH);
					
					//导出成绩排名榜
					$("#ranking-form-show").contents().find("#leadingout-btn").unbind("click");
					$("#ranking-form-show").contents().find("#leadingout-btn").click(function(){
						exportRanking(examcode);
					
					});
					
				}else{
					content += 	"<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#ranking-form-show").contents().find("#result-data-show").empty();
					$("#ranking-form-show").contents().find("#result-data-show").append(content);
				}
			}else{
				processError("查询失败");
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#ranking-form-show").contents().find("#result-data-show").empty();
				$("#ranking-form-show").contents().find("#result-data-show").append(content);
			}
		},null,null);	
	}
	//点击考试名称查看科目详情
	function getExamResult(examcode,examname,grade,schooltype){
		processBegin("正在运行中···");
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"getOneExamInfo",
				model:"bigexam",
				userKey:userKey,
				data:{
					examcode:examcode,
					grades:grade,
					schooltype:schooltype
				},
				remark:"",						
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("查询成功");
				var data = res.data;
				if(data.length > 0){
					for(var i = 0; i < data.length; i++){
						for(var key in data[i]){
							if(data[i][key] == null || data[i][key] == "null" || data[i][key] == undefined){
								data[i][key] = "";
							}
						}
						content += "<div class=\"table-trs\">" +
							"<div class=\"tables-left\" style='margin-left:15px;width:120px;'>" +
								"<div class=\"table-name\" style='width:120px;' title='"+data[i].coursename+"'>"+substr(data[i].coursename,5) + "</div>" +
								"<div class=\"table-code\" style='width:120px;'></div>" +
							"</div>" +
							"<div class=\"tables-middle\" style='margin-left:25%;'>" +
								"<div class=\"table-code\" style='width:250px;'>开始时间： "+getFormatDateByLong(parseInt(data[i].stime)*1000,"yyyy-MM-dd hh:mm")+"</div>" +
								"<div class=\"table-code\" style='width:250px;'>结束时间： "+getFormatDateByLong(parseInt(data[i].etime)*1000,"yyyy-MM-dd hh:mm")+"</div>" +
							"</div>" +
							"<div class=\"tables-right\">" +
								"<div class=\"table-address\">满分值："+data[i].score+"</div>" +
								"<div class=\"table-oprate\">" +
									"<div class=\"table-score\" examcode=\""+data[i].examcode+"\" coursecode=\""+data[i].coursecode+"\" coursename='"+data[i].coursename+"' title=\"成绩\"></div>" +
									"<div class=\"table-inputResults\" examcode=\""+data[i].examcode+"\" coursecode=\""+data[i].coursecode+"\" coursename='"+data[i].coursename+"' title=\"录入\" style='margin-left:20px'></div>" +
								"</div>" +
							"</div>" +
						"</div>";
					}
					$("#result-form-show").contents().find("#result-data-show").empty();
					$("#result-form-show").contents().find("#result-data-show").append(content);
					
					var cdsH =$("#result-form-show").height() - $("#result-form-show").contents().find(".content-title").height() - 40;
					$("#result-form-show").contents().find(".content-data-show").height(cdsH);
					
					$("#result-form-show").contents().find(".table-trs").unbind("hover");
					$("#result-form-show").contents().find(".table-trs").hover(function(){
						$(this).find(".table-oprate").show();
					},function(){
						$("#result-form-show").contents().find(".table-oprate").hide();
					});
					
					$("#result-form-show").contents().find(".table-score").unbind("click");
					$("#result-form-show").contents().find(".table-score").click(function(){
						var examcode = $(this).attr("examcode");
						var coursecode = $(this).attr("coursecode");
						var coursename = $(this).attr("coursename");
						$("#score-form-show").show();
						$("#right-tables").hide();
						$(".title-search").hide();
						//$("#title-search-score").show();
						$("#score-form-show").contents().find("#table-title").html(examname + coursename + "成绩单");
						
						getScoreResult(examcode,coursecode,grade,schooltype);
						
						$("#score-form-show").contents().find("#back-to-table").click(function(){
							$("#score-form-show").hide();
							$("#right-tables").show();
							$(".title-search").hide();
						});
					});
					
					$("#result-form-show").contents().find(".table-inputResults").unbind("click");
					$("#result-form-show").contents().find(".table-inputResults").click(function(){
						var examcode = $(this).attr("examcode");
						var coursecode = $(this).attr("coursecode");
						var coursename = $(this).attr("coursename");
						$("#inputscore-form-show").show();
						$("#right-tables").hide();
						$("#inputscore-form-show").contents().find("#table-title").html(examname + coursename + "成绩录入");
						
						getInputScore(schooltype,examcode,coursecode);
						
						$("#inputscore-form-show").contents().find("#back-to-table").unbind("click");
						$("#inputscore-form-show").contents().find("#back-to-table").click(function(){
							$("#right-tables").show();
							$("#inputscore-form-show").hide();
							$(".title-search").hide();
						});
					});
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#result-form-show").contents().find("#result-data-show").empty();
					$("#result-form-show").contents().find("#result-data-show").append(content);
				}
			}else{
				processError("查询失败");
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#result-form-show").contents().find("#result-data-show").empty();
				$("#result-form-show").contents().find("#result-data-show").append(content);
			}
		},null,null);
	}
	
	//成绩录入
	function getInputScore(schooltype,examcode,coursecode){
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $("#exam-classcode option:selected").val();
		var data = {
				cmd:"getTeahcerAllStudent",
				model:"bigexam",
				userKey:userkey,
				data:{
					examcode:examcode,
					schooltype:schooltype,
					schoolcode:schoolcode,
					classcode:classcode,
					coursecode:coursecode
					
				},
				remark:"",	
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("查询成功");
				var data = res.data.data;
				if(data.length > 0){
					content += "<table class='inputresults-table'>";
					for(var i = 0; i < data.length; i++){
						content += "<tr><td>"+data[i].studentname+"</td><td>"+data[i].studentcode+"</td><td>"+data[i].grades+"年级"+data[i].classes+"班"+"</td><td><input  value='"+data[i].scorenumber+"' name='input-result'></input></td></tr>";	
					}
					content += "</table><div class='add-stu-bottom' style='margin-top:35px;'><div class='save-btns' id='save-btns'>保&nbsp;存</div></div>";
					$("#inputscore-form-show").contents().find("#result-data-show").empty();
					$("#inputscore-form-show").contents().find("#result-data-show").append(content);
					
					var cdsH =$("#inputscore-form-show").height() - $("#inputscore-form-show").contents().find(".content-title").height() - 40;
					$("#inputscore-form-show").contents().find(".content-data-show").height(cdsH);
					
					$("#inputscore-form-show").contents().find("#save-btns").unbind("click");
					$("#inputscore-form-show").contents().find("#save-btns").click(function(){
						saveInputResults(examcode,coursecode);
					});
					
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#inputscore-form-show").contents().find("#result-data-show").empty();
					$("#inputscore-form-show").contents().find("#result-data-show").append(content);
				}
			}else{
				processError("查询失败");
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#inputscore-form-show").contents().find("#result-data-show").empty();
				$("#inputscore-form-show").contents().find("#result-data-show").append(content);
			}
		},null,null);
	}
	
	function saveInputResults(examcode,coursecode){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $("#exam-classcode option:selected").val();
		var list = [];
		$("#inputscore-form-show").contents().find(".inputresults-table tr").each(function(){
			var param = {};
			param.schoolcode = schoolcode;
			param.classcode = classcode;
			param.examcode = examcode;
			param.coursecode = coursecode;
			$(this).children().each(function(i){
				if(i == 0){
					param.studentname = $(this).text();
					return true;
				}else if(i == 1){
					param.studentcode = $(this).text();
					return true;
				}else if(i == 2){
					param.classesname = $(this).text();
					return true;
				}else if(i == 3){
					param.result = $(this).find("input[name='input-result']").val();
					return true;
				}
			});
			list.push(param);
		});
		var data = {
				cmd:"teacherEntryResultInfo",
				model:"bigexam",
				userKey:userKey,
				data:JSON.stringify(list),
				remark:"",						
		};
		processBegin("正在保存中···");
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1){
				processEnd("保存成功");
			}else{
				processError("保存失败");
			}
		},null,null);
	}
	
	//点击科目查看该科目的成绩单信息
	function getScoreResult(examcode,coursecode,grade,schooltype){
		processBegin("正在运行中···");
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $("#exam-classcode option:selected").val(); 
		var data = {
				cmd:"getEveryExamInfo",
				model:"bigexam",
				userKey:userKey,
				data:{
					examcode:examcode,
					coursecode:coursecode,
					schoolcode:schoolcode,
					grades:grade,
					schooltype:schooltype,
					classcode:classcode,
				},
				remark:"",						
		};

		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("查询成功");
				var data = res.data.data;
				if(data.length > 0){
					for(var i = 0; i < data.length; i++){
						for(var key in data[i]){
							if(data[i][key] == null || data[i][key] == "null" || data[i][key] == undefined){
								data[i][key] = "";
							}
						}
						content += "<div class=\"table-trs\">" +
							"<div class=\"tables-left\" style='margin-left:15px;width:120px;'>" +
								"<div class=\"table-name\" style='width:120px;' title='"+data[i].studentname+"'>"+substr(data[i].studentname,5) + "</div>" +
								"<div class=\"table-code\" style='width:120px;'>"+data[i].studentcode+"</div>" +
							"</div>" +
							"<div class=\"tables-middle\" style='margin-left:25%;'>" +
								"<div class=\"table-class\" style='width:200px;'>分数： "+data[i].scorenumber+"</div>" +
								"<div class=\"table-code\" style='width:200px;'>"+data[i].classesname+"</div>" +
							"</div>" +
							"<div class=\"tables-right\">" +
								"<div class=\"table-address\">校排名："+data[i].schoolrank+"</div>" +
								"<div class=\"table-address\">班内排名："+data[i].classranking+"</div>" +
							"</div>" +
						"</div>";
					}
					$("#score-form-show").contents().find("#result-data-show").empty();
					$("#score-form-show").contents().find("#result-data-show").append(content);
					
					var cdsH =$("#score-form-show").height() - $("#score-form-show").contents().find(".content-title").height() - 40;
					$("#score-form-show").contents().find(".content-data-show").height(cdsH);
					
					//导出成绩单
					$("#score-form-show").contents().find("#leadingout-btn").unbind("click");
					$("#score-form-show").contents().find("#leadingout-btn").click(function(){
						exportScore(examcode,coursecode,grade,schooltype,classcode);
					
					});
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#score-form-show").contents().find("#result-data-show").empty();
					$("#score-form-show").contents().find("#result-data-show").append(content);
				}
			}else{
				processError("查询失败");
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#score-form-show").contents().find("#result-data-show").empty();
				$("#score-form-show").contents().find("#result-data-show").append(content);
			}
		},null,null);
	}
	
	function check2(){
		var flg = false;
		var checkFails = $("#exam-form-show").contents().find(".check-fail");
		for(var i = 0; i < checkFails.length; i++){
			if(checkFails[i].innerHTML != ""){
				flg = false;
				break;
			}else{
				flg = true;
			}
		}
		var checkCont = $("#exam-form-show").contents().find(".info-error");
		for(var i = 0; i < checkCont.length; i++){
			if(checkCont[i].innerHTML != ""){
				flg = false;
				break;
			}else{
				flg = true;
			}
		}
		return flg;
	}

	function checkSave(){
		var flag = false;
		var parame = ["examname","classcode","describe","stime","etime","coursecode","score"];
		for(key in parame){
			if(key == "1"){
				if($("#exam-form-show").contents().find(".classcode-item input:checked").length == 0){
					$("#exam-form-show").contents().find("#exam-classcode").focus();
					$("#exam-form-show").contents().find("#exam-classcode-ex").text("不能为空！");
					return false;
				}else{
					$("#exam-form-show").contents().find("#exam-classcode-ex").text("");
					flag = true;
				};
			}else if(key == "3" || key == "4" || key == "5" || key == "6" ){
				$("#exam-form-show").contents().find(".add-info-edit").each(function(){
					var index = $(this).attr("index");
					$(this).children().each(function(){
						if(this.value == ""){
							$("#exam-form-show").contents().find("#info-error" + index).html("不能为空！");
							return false;
						}else{
							$("#exam-form-show").contents().find("#info-error" + index).html("");
							flag = true;
						};
					});
				});
			}else{
				if($("#exam-form-show").contents().find("#exam-" + parame[key]).val() == ""){
					$("#exam-form-show").contents().find("#exam-" + parame[key]).focus();
					$("#exam-form-show").contents().find("#exam-" + parame[key] + "-ex").text("不能为空！");
					return false;
				}else{
					flag = true;
				};
			};
		}
		return flag;
	}
	
	function exportRanking(examcode){
		var grade = $("#exam-classcode option:selected").attr("grade");
		var classcode = $("#exam-classcode option:selected").val();
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		window.frames["export"].location = serverUrl.ExportUrl + "?t=" + Math.random() + "&cmd=getExamTotalList" + "&examcode=" + examcode + "&schoolcode=" + schoolcode + "&classcode=" + classcode + "&grades=" + grade + "&fileName=" + new Date().getTime();
	}
	
	function exportScore(examcode,coursecode,grade,schooltype,classcode){
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		window.frames["export"].location = serverUrl.ExportUrl + "?t=" + Math.random() + "&cmd=getEveryExamInfo" + "&coursecode=" + coursecode + "&examcode=" + examcode + "&schoolcode=" + schoolcode + "&schooltype=" + schooltype + "&grades=" + grade + "&fileName=" + new Date().getTime() + "&classcode=" + classcode;
	}
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	
	
	function pagebtns(){
		$("#exam-search-btn").click(function(){
			now = 0;
			initExam(1,pageSize);
		});		
		$("#save-btns").click(function(){
			saveScore();
		});
	
	}
	
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();